package com.koron.order.overhaulExperimentRel.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.common.core.util.StringUtils;
import com.koron.order.overhaulExperimentRel.bean.po.OverhaulExperimentRelPo;
import com.koron.order.overhaulExperimentRel.mapper.OverhaulExperimentRelMapper;
import com.koron.order.overhaulExperimentRel.service.OverhaulExperimentRelService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 大修工单与试验工单的关联表(OverhaulExperimentRel)表服务实现类
 *
 * @author zhouj
 * @since 2022-04-21 11:20:49
 */
@Service("overhaulExperimentRelService")
public class OverhaulExperimentRelServiceImpl implements OverhaulExperimentRelService {

    @Override
    @TaskAnnotation("insertOrUpdate")
    public int insertOrUpdate(SessionFactory factory, OverhaulExperimentRelPo overhaulExperimentRel) {

        OverhaulExperimentRelMapper mapper = factory.getMapper(OverhaulExperimentRelMapper.class);

        String experimentId = overhaulExperimentRel.getExperimentId();

        Assert.isTrue(!StringUtils.isEmpty(experimentId));

        String[] split = experimentId.split(",");
        List<OverhaulExperimentRelPo> list = new ArrayList<>(split.length);
        for (String id : split) {
            list.add(new OverhaulExperimentRelPo(overhaulExperimentRel.getOverhaulId(), id));
        }
        if (!CollectionUtils.isEmpty(list)) {
            mapper.deleteByOverhaulId(list.get(0).getOverhaulId(), null);
        }
        return insertBatch(factory, list);
    }

    @Override
    @TaskAnnotation("insertOrUpdateBatch")
    public int insertOrUpdateBatch(SessionFactory factory, List<OverhaulExperimentRelPo> overhaulExperimentRelList) {

        OverhaulExperimentRelMapper mapper = factory.getMapper(OverhaulExperimentRelMapper.class);

        return mapper.insertOrUpdateBatch(overhaulExperimentRelList);
    }

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, OverhaulExperimentRelPo overhaulExperimentRel) {

        OverhaulExperimentRelMapper mapper = factory.getMapper(OverhaulExperimentRelMapper.class);

        return mapper.insert(overhaulExperimentRel);
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(SessionFactory factory, List<OverhaulExperimentRelPo> overhaulExperimentRelList) {

        if (CollectionUtils.isEmpty(overhaulExperimentRelList)) {
            return 0;
        }

        OverhaulExperimentRelMapper mapper = factory.getMapper(OverhaulExperimentRelMapper.class);

        return mapper.insertBatch(overhaulExperimentRelList);
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, OverhaulExperimentRelPo overhaulExperimentRel) {
        OverhaulExperimentRelMapper mapper = factory.getMapper(OverhaulExperimentRelMapper.class);
        return mapper.update(overhaulExperimentRel);
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, Integer id) {

        OverhaulExperimentRelMapper mapper = factory.getMapper(OverhaulExperimentRelMapper.class);
        return mapper.deleteById(id);
    }

    @Override
    @TaskAnnotation("deleteByOverhaulId")
    public int deleteByOverhaulId(SessionFactory factory, OverhaulExperimentRelPo overhaulExperimentRel) {
        OverhaulExperimentRelMapper mapper = factory.getMapper(OverhaulExperimentRelMapper.class);
        return mapper.deleteByOverhaulId(overhaulExperimentRel.getOverhaulId(), overhaulExperimentRel.getExperimentId());
    }

    @Override
    @TaskAnnotation("queryById")
    public OverhaulExperimentRelPo queryById(SessionFactory factory, Integer id) {

        OverhaulExperimentRelMapper mapper = factory.getMapper(OverhaulExperimentRelMapper.class);

        return mapper.queryById(id);
    }

    @Override
    @TaskAnnotation("queryList")
    public List<OverhaulExperimentRelPo> queryList(SessionFactory factory, OverhaulExperimentRelPo overhaulExperimentRel) {

        OverhaulExperimentRelMapper mapper = factory.getMapper(OverhaulExperimentRelMapper.class);

        List<OverhaulExperimentRelPo> list = mapper.queryList(overhaulExperimentRel);

        return list;
    }

    @Override
    @TaskAnnotation("queryPageList")
    public PageInfo queryPageList(SessionFactory factory, OverhaulExperimentRelPo overhaulExperimentRel, PageQuery pageQuery) {

        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize(), pageQuery.getOrderBy());
        OverhaulExperimentRelMapper mapper = factory.getMapper(OverhaulExperimentRelMapper.class);

        List<OverhaulExperimentRelPo> list = mapper.queryList(overhaulExperimentRel);

        return new PageInfo(list);
    }

}